#include <QtCore>
#include <QtGui>
#include <QtNetwork>
#include "ui_ui.h"

class MegaUploadDecrypter: public QWidget
{
    Q_OBJECT
public:
    MegaUploadDecrypter()
    : nm(0)
    {
        settings = new QSettings("Brandon", "Megadecrypter");
        loggedIn = false;
        ui.setupUi(this);
        ui.leUsername->setText(settings->value("username").toString());
        ui.lePassword->setText(settings->value("password").toString());

        nm = new QNetworkAccessManager(this);
        connect(nm, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
        connect(ui.btnDecrypt, SIGNAL(clicked()), this, SLOT(decrypt()));
        connect(ui.leUsername, SIGNAL(textEdited(QString)), this, SLOT(saveSettings()));
        connect(ui.lePassword, SIGNAL(textEdited(QString)), this, SLOT(saveSettings()));
    }
    
    bool decryptLink(QString link)
    {
        QRegExp rx("\\[DOWNLOADS\\]:(.*)\\s");
        rx.setMinimal(true);
        
        if(link.contains(rx)) {
            QNetworkRequest req(QUrl("http://www.megauploadforum.net/redirect.php?"+rx.cap(1)));
            req.setRawHeader("User-Agent" ,"Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6 GTB5");
            nm->get(req);
            return true;
        }
        ls.removeFirst();
        return false;
    }
public slots:
    void saveSettings()
    {
        settings->setValue("username", ui.leUsername->text());
        settings->setValue("password", ui.lePassword->text());
    }

    void timerDecrypt()
    {
        while(!ls.isEmpty() && !decryptLink(ls.at(0)));
    }

    void replyFinished(QNetworkReply* reply)
    {
        if(sid.isEmpty()) {
            QString ba = reply->readAll();
            QRegExp rx("name=\"sid\" value=\"(.*)\"");
            rx.setMinimal(true);
            if(ba.contains(rx)) {
                sid = rx.cap(1);
                decrypt();
            }
            return;
        }

        if(!loggedIn) {
            loggedIn = true;
            decrypt();
            return;
        }

        if(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == 302) {
            ui.decryptedText->appendPlainText(reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl().toString());
            ls.removeFirst();
            ui.progressBar->setValue(ui.progressBar->value()+1);
        }

        QTimer::singleShot(11000, this, SLOT(timerDecrypt()));
    }

    void decrypt()
    {
        if(sid.isEmpty()) {
            QNetworkRequest req(QUrl("http://www.megauploadforum.net/ucp.php?mode=login"));
            req.setRawHeader("User-Agent" ,"Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6 GTB5");
            req.setRawHeader("Accept","text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
            req.setRawHeader("Keep-Alive", "300");
            req.setRawHeader("Connection", "keep-alive");

            nm->get(req);
            return;
        }

        if(!loggedIn) {
            QNetworkRequest req(QUrl("http://www.megauploadforum.net/ucp.php?mode=login"));
            req.setRawHeader("User-Agent" ,"Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6 GTB5");
            req.setRawHeader("Accept","text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
            req.setRawHeader("Keep-Alive", "300");
            req.setRawHeader("Connection", "keep-alive");
            req.setRawHeader("Referer", "http://www.megauploadforum.net/ucp.php?mode=login");
            req.setRawHeader("Content-type", "application/x-www-form-urlencoded");

            QByteArray data = QString("username="+ui.leUsername->text()+"&password="+ui.lePassword->text()+"&redirect=index.php&sid="+sid+"&login=Login").toUtf8();

            QByteArray clength = QByteArray::number(data.length());
            req.setRawHeader("Content-Length", clength);

            nm->post(req, data);
            return;
        }

        QString plaintext = ui.cryptedText->toPlainText();
        ls = plaintext.split("\n").filter("DOWNLOADS");
        ui.progressBar->setMaximum(ls.size());
        
        while(!ls.isEmpty() && !decryptLink(ls.at(0)));
    }

private:
    Ui_Dialog ui;    
    QStringList ls;
    QNetworkAccessManager *nm;
    bool loggedIn;
    QString sid;
    QSettings *settings;
};

int main(int argc, char **argv)
{
    QApplication app(argc, argv);
    MegaUploadDecrypter mw;
    mw.show();
    return app.exec();
    
}


#include "main.moc"